// components/carousel_widget.dart
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:cesi/model/Coffee.dart';
import 'package:cesi/theme/color.dart';

class CoffeeCarousel extends StatefulWidget {
  final List<Coffee> coffeeList;
  final Function(Coffee) onTap;

  const CoffeeCarousel({
    super.key,
    required this.coffeeList,
    required this.onTap,
  });

  @override
  State<CoffeeCarousel> createState() => _CoffeeCarouselState();
}

class _CoffeeCarouselState extends State<CoffeeCarousel> {
  final PageController _pageController = PageController(viewportFraction: 0.8);
  int _currentPage = 0;
  Timer? _timer;

  @override
  void initState() {
    super.initState();
    _startAutoPlay();
  }

  @override
  void dispose() {
    _timer?.cancel();
    _pageController.dispose();
    super.dispose();
  }

  void _startAutoPlay() {
    _timer = Timer.periodic(const Duration(seconds: 3), (timer) {
      if (_pageController.hasClients) {
        if (_currentPage < widget.coffeeList.length - 1) {
          _currentPage++;
        } else {
          _currentPage = 0;
        }
        _pageController.animateToPage(
          _currentPage,
          duration: const Duration(milliseconds: 500),
          curve: Curves.easeInOut,
        );
      }
    });
  }

  void _onPageChanged(int page) {
    setState(() {
      _currentPage = page;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (widget.coffeeList.isEmpty) {
      return Container(
        height: 120,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20),
        ),
        child: Center(
          child: Text(
            "暂无推荐商品",
            style: TextStyle(color: AppColors.textLight, fontSize: 16),
          ),
        ),
      );
    }

    return Column(
      children: [
        // 轮播标题
        Padding(
          padding: const EdgeInsets.only(left: 25, right: 25, bottom: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "热门推荐",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 18,
                  color: AppColors.textDark,
                ),
              ),
              Row(
                children: [
                  Text(
                    "${_currentPage + 1}",
                    style: TextStyle(
                      color: AppColors.primaryBrown,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Text(
                    " / ${widget.coffeeList.length}",
                    style: TextStyle(color: AppColors.textLight),
                  ),
                ],
              ),
            ],
          ),
        ),

        // 轮播内容
        SizedBox(
          height: 140,
          child: PageView.builder(
            controller: _pageController,
            onPageChanged: _onPageChanged,
            itemCount: widget.coffeeList.length,
            itemBuilder: (context, index) {
              final coffee = widget.coffeeList[index];
              return GestureDetector(
                onTap: () => widget.onTap(coffee),
                child: Container(
                  margin: const EdgeInsets.symmetric(horizontal: 10),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20),
                    boxShadow: [
                      BoxShadow(
                        color: AppColors.lightBrown.withOpacity(0.3),
                        blurRadius: 10,
                        offset: const Offset(0, 4),
                      ),
                    ],
                  ),
                  child: Row(
                    children: [
                      // 商品图片
                      Container(
                        width: 100,
                        height: 100,
                        margin: const EdgeInsets.all(15),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),
                          image: DecorationImage(
                            image: AssetImage(coffee.imagePath),
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),

                      // 商品信息
                      Expanded(
                        child: Padding(
                          padding: const EdgeInsets.all(15),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                coffee.name,
                                style: TextStyle(
                                  fontSize: 18,
                                  color: AppColors.textDark,
                                  fontWeight: FontWeight.bold,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                              const SizedBox(height: 5),
                              Row(
                                children: [
                                  Icon(
                                    Icons.star,
                                    color: AppColors.caramel,
                                    size: 16,
                                  ),
                                  const SizedBox(width: 4),
                                  Text(
                                    coffee.rating,
                                    style: TextStyle(
                                      color: AppColors.textLight,
                                      fontSize: 14,
                                    ),
                                  ),
                                ],
                              ),
                              const SizedBox(height: 8),
                              Text(
                                '\$${coffee.price}',
                                style: TextStyle(
                                  color: AppColors.primaryBrown,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 16,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),

                      // 收藏图标
                      Padding(
                        padding: const EdgeInsets.only(right: 15, top: 10),
                        child: Icon(
                          Icons.favorite_outline,
                          color: AppColors.mediumBrown,
                          size: 24,
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        ),

        // 指示器
        const SizedBox(height: 10),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: List.generate(
            widget.coffeeList.length,
            (index) => Container(
              width: 8,
              height: 8,
              margin: const EdgeInsets.symmetric(horizontal: 4),
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: _currentPage == index
                    ? AppColors.primaryBrown
                    : AppColors.lightBrown,
              ),
            ),
          ),
        ),
      ],
    );
  }
}
